package com.zd.async;


import com.zd.model.WorkInfo;
import com.zd.service.impl.WorkInfoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * 定时器任务类
 */
@Component
public class AsyncTimer {

    @Autowired
    WorkInfoServiceImpl service;

    /**
     * 查找即将协议签订的到期时间
     * 规则 当前时间<=协议签订结束时间
     * 改变提醒字段
     */
    //每分钟启动
     /*@Scheduled(cron = "0 0/1 * * * ?")*/
  /*  //每5秒启动
    @Scheduled(cron = "0/5 * *  * * ? ")*/
  //凌晨0点启动
    @Scheduled(cron = "0 0 0 * * ?")
    public void findSignTimeEnd() {
        Date date;
        Calendar c = Calendar.getInstance();
        c.set(Calendar.MONTH, c.get(Calendar.MONTH) + 1); // 设置为下个月时间
        date = c.getTime();//获取下个月时间
        List<WorkInfo> list = service.selectMaturity();
        for (WorkInfo tmp : list) {
            if (tmp.getExpirationtime().before(date)) {
                if (tmp.getExpirationtips()==0){
                    tmp.setExpirationtips(1);
                    service.updateSelective(tmp);
                }
                /*System.out.println(tmp.getExpirationtips() + "提醒");*/
            } else {
                if (tmp.getExpirationtips()==2){

                }else{
                    //防止手滑改了数据库Expirationtips，自动纠正
                    tmp.setExpirationtips(0);
                    service.updateSelective(tmp);
                }
                /*System.out.println(tmp.getExpirationtips() + "不提醒");*/
            }
        }
    }
}
